package com.warren.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.warren.annotation.ControllerEndpoint;
import com.warren.response.Result;
import com.warren.system.entity.File;
import com.warren.system.service.FileService;
import com.warren.system.vo.FileVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author warren
 * @since 2021-04-01
 */
@Api(value = "文件操作",tags = "文件上传删除下载")
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @ApiOperation(value = "添加文件",notes = "添加文件")
    @ControllerEndpoint(exceptionMessage = "添加文件记录失败",operation = "添加文件记录")
    @PostMapping("/addFile")
    public Result addFile(@RequestBody File file){
        try {
            fileService.save(file);
            return Result.ok().message("添加成功");
        } catch (Exception exception) {
            return Result.error().message("添加失败");
        }
    }

    @ControllerEndpoint(exceptionMessage = "删除文件失败",operation = "删除文件记录")
    @ApiOperation(value = "删除文件")
    @PostMapping("/deleteFileById")
    public Result deleteFileById(Long id){

        try {
            fileService.removeById(id);
            return Result.ok().message("删除成功");
        } catch (Exception exception) {
            return Result.error().message("删除失败");
        }

    }

    @ApiOperation(value = "删除文件",notes = "根据url删除文件")
    @ControllerEndpoint(exceptionMessage = "删除文件失败",operation = "根据url删除文件")
    @PostMapping("/deleteFileByUrl")
    public Result deleteFileByUrl(String fileUrl){
        QueryWrapper<File> queryWrapper = new QueryWrapper<>();
        if (fileUrl != null){
            queryWrapper.eq("url",fileUrl);
        }

        try {
            fileService.remove(queryWrapper);
            return Result.ok();
        } catch (Exception exception) {
            return Result.error();
        }
    }

    @ApiOperation(value = "分页查询文件")
    @PostMapping("/findFilePage")
    public Result findFilePage(@RequestParam(defaultValue = "1") Integer current,
                               @RequestParam(defaultValue = "6") Integer size,
                               @RequestBody FileVO fileVO){
        //取得分页条件
        Page<File> page = new Page<>(current,size);
        //获得wragger
        QueryWrapper<File> queryWrapper = getWrapper(fileVO);

        //执行查询
        IPage<File> fileIPage = fileService.page(page,queryWrapper);

        long total = fileIPage.getTotal();
        List<File> records = fileIPage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }

    @ApiOperation(value = "批量删除文件",notes = "根据ids删除")
    @PostMapping("/deleteFilesByIds")
    public  Result deleteFilesByIds(String ids){
        String[] idList = ids.split(",");
        boolean flag = fileService.removeByIds(Arrays.asList(idList));
        if (flag){
            return Result.ok().message("批量删除成功");
        }else{
            return Result.ok().message("批量删除失败");
        }

    }

    @ApiOperation(value = "批量删除文件",notes = "根据url删除")
    @PostMapping("/deleteFilesByUrls")
    public Result deleteFilesByUrls(String fileUrls){
        String[] urlList = fileUrls.split(",");
        if(urlList.length > 0){
            for (String s : urlList){
                QueryWrapper<File> queryWrapper = new QueryWrapper<>();
                if (s != null){
                    queryWrapper.eq("url",s);
                }

                try {
                    fileService.remove(queryWrapper);
                } catch (Exception e) {
                    return Result.error().message("删除失败");
                }
            }
        }
        return Result.ok().message("删除成功");
    }

    private QueryWrapper<File> getWrapper(FileVO fileVo) {
        QueryWrapper<File> queryWrapper = new QueryWrapper<>();
        if (fileVo != null) {
            //查询每个条件
            if (!ObjectUtils.isEmpty(fileVo.getFileName())) {
                queryWrapper.like("file_name", fileVo.getFileName());
            }
            if (!ObjectUtils.isEmpty(fileVo.getType())) {
                queryWrapper.like("type", fileVo.getType());
            }
            //模糊查询
            if (!ObjectUtils.isEmpty(fileVo.getUrl())) {
                queryWrapper.like("url", fileVo.getUrl());
            }
        }
        return queryWrapper;
    }
}

